package vue;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import DB.Personne;

import modele.Batiment;
import vue.actions.dialogBadge;
import vue.actions.dialogEntree;
import vue.actions.dialogIncendie;
import vue.actions.dialogJournal;
import vue.actions.dialogSortie;
import vue.composants.Boutton;
import controleur.AccesControleur;

public class FenetrePrincipale extends JFrame implements ActionListener{

	private static final long serialVersionUID = 1L;
	private AccesControleur controleur;
	private JPanel pannelPrincipal;
	private JPanel pannelSysteme;
	private JPanel pannelActions;
	private JLabel labelSysteme;
	private JLabel labelActions;
	private Boutton bouttonEntree;
	private Boutton bouttonSortie;
	private Boutton bouttonBadge;
	private Boutton bouttonJournal;
	private Boutton bouttonIncendie;
	private JTable tableSysteme;
	
	public FenetrePrincipale()
	{
		super();
		this.setControleur(new AccesControleur());
		this.initialiseFenetre();
	}
	
	/**
	 * Fonction qui initialise la fenetre principale. Instancie tous les composants graphiques.
	 */
	private void initialiseFenetre()
	{
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(1000, 600);
		
		this.pannelSysteme = new JPanel();
		this.pannelSysteme.setLayout(new GridLayout(2,1));
		this.labelSysteme = new JLabel("Etat du système : ");
		this.labelSysteme.setSize(200,200);
		this.pannelSysteme.add(this.labelSysteme);
		this.affichageSystemeCourant();
		
		this.pannelActions = new JPanel();
		this.labelActions = new JLabel("Actions :");
		
		this.bouttonEntree = new Boutton("Entrée");
		this.bouttonEntree.addActionListener(this);
		
		this.bouttonSortie = new Boutton("Sortie");
		this.bouttonSortie.addActionListener(this);
		
		this.bouttonBadge = new Boutton("Badge");
		this.bouttonBadge.addActionListener(this);
		
		this.bouttonJournal = new Boutton("Journal");
		this.bouttonJournal.addActionListener(this);
		
		this.bouttonIncendie = new Boutton("Icendie");
		this.bouttonIncendie.addActionListener(this);
		
		this.pannelActions.add(this.labelActions);
		this.pannelActions.add(this.bouttonEntree);
		this.pannelActions.add(this.bouttonSortie);
		this.pannelActions.add(this.bouttonBadge);
		this.pannelActions.add(this.bouttonJournal);
		this.pannelActions.add(this.bouttonIncendie);
		
		
		
		this.pannelPrincipal = new JPanel();
		this.pannelPrincipal.setLayout(new GridLayout(1,2));
		this.pannelPrincipal.add(this.pannelActions);
		this.pannelPrincipal.add(this.pannelSysteme);
		
		this.getContentPane().setLayout(new GridLayout());
		this.getContentPane().add(this.pannelPrincipal);
		
		this.setVisible(true);
	}
	
	
	/**
	 * Fonction qui permet d'afficher les informations du système courant
	 */
	private void affichageSystemeCourant()
	{
		int i = 0;
		String[] colName = {"Batiments","Personnes"};
	    		
		i=this.controleur.getPersonnes().size();
		if(this.controleur.getPersonnes().size()<this.controleur.getBatiments().size())i=this.controleur.getBatiments().size();
		
		for(Batiment b : this.controleur.getBatiments())
		{
			for(@SuppressWarnings("unused") Personne p : b.getPersonnes())
			{
				i++;
			}
		}
		Object valeurTable[][] = new Object[i][2];
		
		i=0;
		for(Batiment b : this.controleur.getBatiments())
		{
			valeurTable[i++][0]=b.getNom();
			for(Personne p : b.getPersonnes())
			{
				valeurTable[i++][1]=p.toString();
			}
		}
		
		this.tableSysteme = new JTable(valeurTable,(Object[])colName);
  
		this.pannelSysteme.add(new JScrollPane(tableSysteme));
		this.pannelSysteme.setSize(300,150);
	}
	
	@Override
	public void actionPerformed(ActionEvent event) {
		if(event.getSource()==this.bouttonEntree)
		{
			this.entrer();
		}
		else if(event.getSource()==this.bouttonSortie)
		{
			this.sortir();
		}
		else if(event.getSource()==this.bouttonIncendie)
		{
			this.incendie();
		}
		else if(event.getSource()==this.bouttonJournal)
		{
			this.journal();
		}
		else if(event.getSource()==this.bouttonBadge)
		{
			this.badge();
		}
	}
	
	private void entrer()
	{
		new dialogEntree(this);
	}

	private void sortir()
	{
		new dialogSortie(this);
	}

	private void incendie()
	{
		new dialogIncendie(this);
	}

	private void journal()
	{
		new dialogJournal(this);
	}

	private void badge()
	{
		new dialogBadge(this);
	}


	/**
	 * @return the controleur
	 */
	public AccesControleur getControleur() {
		return controleur;
	}



	/**
	 * @param controleur the controleur to set
	 */
	public void setControleur(AccesControleur controleur) {
		this.controleur = controleur;
	}
	
	@SuppressWarnings("unused")
	public static void main(String[] args) 
	{
		FenetrePrincipale fp = new FenetrePrincipale();
	}
}
